/**
 * 区域
 */

//主要是验证框架
var TaskModule = function () {
	var taskId = "";
	var taskTable = function(){
		var timestamp = new Date().getTime();
		var oTableInit = new Object();
		oTableInit.Init = function() {
			var tableList = $("#record_user_list").bootstrapTable(
				{
					url :projectName + "/user/getTempUser",
					method : "post",
					contentType : "application/x-www-form-urlencoded",//必须的
					striped : true, //使表格带有条纹
					cache: false,
					pagination : true, //在表格底部显示分页工具栏;
					pageSize : 50,
					pageNumber : 1,
					pageList : [10,20,50,100,200,500],
					idField : "id", //标识哪个字段为id主键
					showToggle : false, //名片格式
					cardView : false,//设置为True时显示名片（card）布局
					showColumns : false, //显示隐藏列
					//showRefresh: true,  //显示刷新按钮
					singleSelect : true,//复选框只能选择一条记录
					search : false,//是否显示右上角的搜索框
					clickToSelect : true,//点击行即可选中单选/复选框
					showRefresh: true,
					sidePagination : "server",//表格分页的位置
					queryParams : queryParams, //参数
					//queryParamsType: "limit", //参数格式,发送标准的RESTFul类型的参数请求
					//toolbar: "#toolbar", //设置工具栏的Id或者class
					//columns: column, //列
					silent : true, //刷新事件必须设置
					ajaxOptions:{
						beforeSend:function(XMLHttpRequest){
							var tokenCode = sessionStorage.getItem("tokenCode");
							XMLHttpRequest.setRequestHeader("token", tokenCode);
						}
					},
					columns : [
						{
							field : "userTel",
							title : "手机号"
						},
						{
							field : "addTime",
							title : "新增时间"
						},
						{
							field : "nickname",
							title : "昵称"
						},
						{
							field : "insertStatus",
							title : "是否插入主库",
							formatter : function(value,row, index) {
								var result = "";
								if(row.insertStatus==="1"){
									result += "<span class='text-success'>已插入</span>";
								}else {
									result += "<span class='test-info'>未插入</span>";
								}
								return[
									result,
								].join("");
							},
						},
						{
							field : "mainTime",
							title : "插入主库时间"
						},
						{
							field : "isCertified",
							title : "操作",
							formatter : function(value,row, index) {
								return[
									"<button class='btn btn-xs btn-info edit' id='opr_edit_" + row.id +"'>修改</button>",
									//"<button class='btn btn-xs btn-danger remove' id='opr_del_" + row.id +"'>删除</button>",
								].join("");
							},
							events:{
								'click .edit': function (e, value, row, index) {
									$("#task_unitValue").val(row.unitValue);
									$("#task_unitEnergy").val(row.unitEnergy);
									$("#task_ceiling_energy").val(row.dailyCeilingEnergy);
									$("#description").val(row.description);
									taskId = row.id;
									$("#task_modal_update").modal("show");
								},
								'click .remove': function (e, value, row, index) {
									bootbox.confirm({
										buttons:{
											confirm: {
												label: '确认删除',
												className: 'btn btn-xs btn-danger'
											},
											cancel: {
												label: '取消',
												className: 'btn btn-xs btn-info'
											}
										},
										message:'确定需要删除['+ row.content +"]的题目么?",

										callback: function(result) {
											if(result) {
												sendRequest.withOutToken({
													url:projectName + "/question/delete",
													type:"POST",
													data:{id:row.id},
													callback:function (obj) {
														if(obj.code==="1"){
															$("#question_list").bootstrapTable("refresh");
														}
													}
												});
												$("#question_list").bootstrapTable("refresh");
											} else {
											}
										},

									});

								},
							}
						},
					],
					onClickRow: function (row, $element) {
						curRow = row;
					},
					onLoadSuccess: function (aa, bb, cc) {
					},
				});
			return tableList;
		}
		return oTableInit;
	}
	function queryParams(params) {
		// params.insertStatus = $("#insertStatus").find("option:selected").val();
		params.insertStatus = $("#insertStatus  option:selected").val();
		console.log(params);
		return params;
	}
	// var resetInput = function () {
	// 	$("#qus_content").val("");
	// 	$("#answer1_content").val("");
	// 	$("#answer2_content").val("");
	// 	$("#qus_count").val("");
	// 	// $("input[name='answer_select']").removeAttrs("checked");
	// 	var answer0 = document.getElementById("answer0");
	// 	answer0.checked = false;
	// 	var answer1 = document.getElementById("answer1");
	// 	answer1.checked = false;
	// 	$("#ques_type option:first").prop("selected", 'selected');
	// 	$("#ques_reward option:first").prop("selected", 'selected');
	//
	// }
	/**
	 *判断是否是数字
	 *
	 **/

	function isRealNum(val){
		// isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，

		if(val === "" || val ==null){
			return false;
		}
		if(!isNaN(val)){
			//对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
			//所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
			return true;
		}

		else{
			return false;
		}
	}

	var checkTaskAdd = function () {
		var unitValue = $("#task_unitValue").val();
		var unitEnergy = $("#task_unitEnergy").val();
		var ceilingEnergy = $("#task_ceiling_energy").val();
		var description = $("#description").val();

		if(!isRealNum(unitValue)){
			swal("警告","单位值必须为整数!","error");
			return false;
		}
		if(!isRealNum(unitEnergy)){
			swal("警告","单位值所能兑换的能量必须为整数!","error");
			return false;
		}
		if(!isRealNum(ceilingEnergy)){
			swal("警告","每日上限能量值必须为整数!","error");
			return false;
		}
		if(description.length < 1){
			swal("警告","描述不能为空!","error");
			return false;
		}
		var data = {};
		data.taskId = taskId;
		data.unitValue = unitValue;
		data.unitEnergy = unitEnergy;
		data.ceilingEnergy = ceilingEnergy;
		data.description = description;
		sendRequest.withOutToken({
			url:projectName + "/task/updateActiveNum",
			data:data,
			method:"POST",
			callback:function (obj) {
				if(obj.code==="1"){
					$("#task_modal_update").modal("hide");
					$("#task_list").bootstrapTable("refresh");
				}
			}
		})
		// iManager.updateTaskActiveNum({
		//
		// })

	}
	return {
		listInit: function () {
			var oTable = new taskTable();
			var bootstrapTable = oTable.Init();
			$("#add_question").on("click",function () {
				taskId = "";
				$("#ach_modal_add").modal("show");
			})
			$("#task_update").on("click",checkTaskAdd);
			$("#search").on("click",function () {
				$("#record_user_list").bootstrapTable("refresh");
			})
		},
	};
}();

